<?php
namespace Components\User\Sha\Wallet;

use Components\User\Sha\Auth;
use Components\User\Sha\Service as UserService;
use Components\User\Sha\Validation as UserValidation;

class Facade {
  public function pagerByfield($fields_index, $query) {
    if(!isset($query['page'])) {
      $query['page'] = 1;
    } else if($query['page'] <= 0)  {
      $query['page'] = 1;
    } else {
      $query['page'] = abs($query['page']);
    }

    if(!isset($query['limit'])) {
      $query['limit'] = 10;
    } else if($query['limit'] <= 0)  {
      $query['limit'] = 10;
    } else {
      $query['limit'] = abs($query['limit']);
    }

    $field = Constants::getByIndex('user_wallet_record_fields', $fields_index);
    if(!$field) { return ['code'=>-1,'msg'=>'字段不存在或已删除']; }
    
    return (new Logic()) -> pagerByfield($fields_index, $query['limit'], $query['page']);
  }

  public function rechargeByAccount($fields_index, $account, $amount) {
    if(is_null($account) || empty($account)) { return ['code'=>-1,'msg'=>'用户不存在!'];  }

    $resUser = (new UserService()) -> findFirst(['conditions'=>'account = '.$account]);
    if($resUser['code'] != 0) { return ['code'=>-1,'msg'=>'用户不存在!']; }

    return (new Service()) -> rechargeByFieldAndId($fields_index, $resUser['model'] -> id, $amount, Record\Constants::OPERATOR_STAFF, null, null);
  }

  public function transferByFieldsIndexAndToken($fields_index, $token, $data) {
    $checkToken = Auth::checkToken($token);
    if($checkToken['code'] != 0) { return $checkToken; }

    $userService = new UserService();
    $toResUser = $userService -> findFirst(['conditions'=>'account = '.$data['to_account']]);
    if($toResUser['code'] != 0) { return ['code'=>-1,'msg'=>'用户不存在!']; }

    $fromResUser = $userService -> findFirst(['conditions'=>'id = '.$checkToken['payload']['user']['id']]);
    if($fromResUser['code'] != 0) { return $fromResUser; }

    if($fromResUser['model'] -> account === $data['to_account']) { return ['code'=>-1,'msg'=>'不能转账给自己']; }

    $checkSafePassword = (new UserValidation()) -> checkSafePassword($data['safe_password'], $fromResUser['model'] -> safe_password);
    if($checkSafePassword['code'] != 0) { return ['code'=>$checkSafePassword['code'],'msg'=>'安全密码错误']; }

    if($toResUser['model'] -> pid == 0) { return ['code' => -1, 'msg' => '此用户不在您的团队!']; }

    if($toResUser['model'] -> pid != $fromResUser['model'] -> id) {
      $currentToResParentUser = $userService -> findFirst([
        'conditions' => 'id = '.$toResUser['model'] -> pid,
        'columns' => 'id, pid, account, is_center'
      ]);

      while(true) {
        if(
          ($currentToResParentUser['code'] != 0) ||
          ($currentToResParentUser['model'] -> pid <= 0) ||
          ($currentToResParentUser['model'] -> id == $currentToResParentUser['model'] -> pid)
        ) { return ['code' => -1, 'msg' => '此用户不在您的团队!']; }

        if($currentToResParentUser['model'] -> is_center == 1) { return ['code' => -1, 'msg' => '此用户不在您的团队!']; }

        if($currentToResParentUser['model'] -> pid == $fromResUser['model'] -> id) { break; }

        $currentToResParentUser = $userService -> findFirst([
          'conditions' => 'id = '.$currentToResParentUser['model'] -> pid,
          'columns' => 'id, pid, account, is_center'
        ]);
      }
    }

    return (new Service()) -> transferByFieldsIndexAndToken($fields_index, $fromResUser['model'] -> id, $toResUser['model'] -> id, $data['amount'], Record\Constants::OPERATOR_USER, null, null);
  }
}
